package com.bbx.taxi.client.Activity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import butterknife.InjectView;

import com.bbx.androidapi.util.SharedPreUtil;
import com.bbx.api.gclient.msg.CouponMsg;
import com.bbx.api.sdk.model.passanger.Return.Coupon.Coupons;
import com.bbx.api.util.SharedPreEncryptUtil;
import com.bbx.taxi.official.R;
import com.bbx.taxi.client.Activity.Base.BaseBBXActivity;
import com.bbx.taxi.client.Adapter.CouponAdapter;
import com.bbx.taxi.client.Adapter.CouponAdapter.ViewHolder;
import com.bbx.taxi.client.Bean.Extra.UseCouponMsg;

public class UseCouponActivity extends BaseBBXActivity implements CouponMsg,OnItemClickListener{
	@InjectView(R.id.listview)
	ListView listview;
	@InjectView(R.id.layout_nodata)
	LinearLayout layout_nodata;

	private CouponAdapter mCouponAdapter;
	private List<Coupons> list;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_usecoupon);
		super.onCreate(savedInstanceState);
		
	}

	@Override
	protected void initView() {
		setTitle(true,getString(R.string.usbcoupon_title), -1, getString(R.string.coupon_no));
		list = new ArrayList<Coupons>();
		list.addAll(MainActivity.list_available);
		setLine_CN();
		mCouponAdapter = new CouponAdapter(this, list);
		listview.setOnItemClickListener(this);
		Bundle mExtras=getIntent().getExtras();
		if(mExtras!=null){
			mCouponAdapter.setCoupon_id(mExtras.getInt(UseCouponMsg.coupon_id));
		}
		else{
			mCouponAdapter.setCoupon_id(0);
		}
		listview.setAdapter(mCouponAdapter);
	}

	@Override
	protected void initData() {
	}

	@Override
	protected void desotryItems() {
		// TODO Auto-generated method stub

	}

	public void notifyDataSetChanged(BaseAdapter adapter) {
		adapter.notifyDataSetChanged();
		onisShowNodata(adapter);
	}

	public void onisShowNodata(BaseAdapter adapter) {
		if (adapter.getCount() == 0) {
			layout_nodata.setVisibility(View.VISIBLE);
		} else {
			layout_nodata.setVisibility(View.GONE);
		}
	}
    @Override
    public void onClick(View v) {
    	switch(v.getId()){
    	case R.id.tv_right:
    		Intent intent = new Intent();
    		intent.putExtra(UseCouponMsg.sum,"0");
    		intent.putExtra(UseCouponMsg.coupon_id, 0);
    		setResult(UseCouponMsg.resultCode, intent);
    		finish();
    		break;
    	default:
        	super.onClick(v);
    		break;
    	}

    }
    public void setLine_CN(){
    	String str_line_num = SharedPreUtil.getStringValue(this, SharedPreEncryptUtil.list_line_num, "");
    	String str_line_cn = SharedPreUtil.getStringValue(this, SharedPreEncryptUtil.list_line_cn, "");
    	final List<String> list_line_num=Arrays.asList(str_line_num.replace("[","").replace("]", "").trim().split(","));
    	final List<String> list_line_cn=Arrays.asList(str_line_cn.replace("[","").replace("]", "").trim().split(","));
		new Handler().postDelayed(new Runnable() {
			
			@Override
			public void run() {
				for(int z=0;z<list.size();z++){
		    		String name;
		    		if(list.get(z).getMetadata()!=null){
		    			List<String> list_line=list.get(z).getMetadata().getUsableline(); 
		    			if(list_line!=null){
		    				String line="";
		    				for(int i=0;i<list_line.size();i++){
		    					if(list_line.get(i).trim().equals("*")){
		    						line=getString(R.string.line_name_all);
		    						break;
		    					}
		    					else {
		    						for(int j=0;j<list_line_num.size();j++){
		    							if(list_line.get(i).trim().equals(list_line_num.get(j).trim())){
		    								if(i!=0&&list_line.size()!=1){
				    							line+="、";
				    						}	
		    								line=line+list_line_cn.get(j);		
				    						break;
				    					}
		    						}
		    					
		    					}
		    				}
		    				//获取不到线路，则默认全部路线
		    				if(line.equals("")){
		    					name="";
		    				}
		    				else{
		    					name=String.format(getString(R.string.line_name), line);
		    				}
		    				list.get(z).setLine_CN(name);
		    			}
		    		}
		    	}

				for(int i=list.size()-1;i>=0;i--){
					if(list.get(i).getLine_CN()==null){
						list.remove(i);
					}else if(list.get(i).getLine_CN()!=null&&list.get(i).getLine_CN().equals("")){
						list.remove(i);
					}
				}
				notifyDataSetChanged(mCouponAdapter);
			}
		}, 10);
    	
    	
    }

	@Override
	public void onItemClick(AdapterView<?> arg0, final View v, final int position, long arg3) {
		mCouponAdapter.setCoupon_id(0);
		mCouponAdapter.notifyDataSetChanged();
		new Handler().postDelayed(new Runnable() {
			
			@Override
			public void run() {
				ViewHolder vHollder = (ViewHolder) v.getTag();
				vHollder.layout_top.setBackgroundDrawable(getResources().getDrawable(R.drawable.yhq_bg_down));
				Intent intent = new Intent();
				intent.putExtra(UseCouponMsg.sum, list.get(position).getSum());
				intent.putExtra(UseCouponMsg.coupon_id, list.get(position).getId());
				setResult(UseCouponMsg.resultCode, intent);
				finish();
			}
		}, 10);

	}
}
